package com.example.a2032.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.WindowManager;


import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.example.a2032.MainActivity;
import com.example.a2032.Utils.ResolveUtil;
import com.example.a2032.Utils.RxBus;
import com.example.a2032.Utils.SerialPortUtil;
import com.example.a2032.Utils.Utils;
import com.example.a2032.Utils.WebSocketUtils;
import com.example.a2032.activity.dfu.OtaActivity;
import com.example.a2032.db.DataDbHelper;
import com.example.a2032.listener.DeviceDataListener;
import com.example.a2032.entity.ActionData;
import com.example.a2032.entity.BleData;
import com.example.a2032.entity.EcgMeasureResult;
import com.example.a2032.entity.PPGMeasureData;
import com.example.a2032.entity.RealTimeStepData;
import com.example.a2032.entity.SleepData;
import com.example.a2032.entity.WatchUserInfo;

import java.util.Calendar;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class BaseActivity extends AppCompatActivity implements DeviceDataListener {
    public static final String KEY_MeasureTime = "KEY_MeasureTime";
    public static final String KEY_MeasureFrom = "KEY_MeasureFrom";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     //   Utils.changeAppLanguage(this);
    }

    protected void startActivity(Class activity) {
        Intent intent = new Intent(this, activity);
        startActivity(intent);
    }

    protected void startActivity(Class activity, String key, String value) {
        if(activity==null)return;
        Intent intent = new Intent(this, activity);
        intent.putExtra(key, value);
        startActivity(intent);
    }
    protected void startActivity(Class activity, String key, int value) {
        if(activity==null)return;
        Intent intent = new Intent(this, activity);
        intent.putExtra(key, value);
        startActivity(intent);
    }

    protected void sendData(byte[] value) {
        SerialPortUtil.getInstance().sendSerialPort(value);
    }

    Disposable disposable;
    private static final String TAG = "BaseActivity";

    protected void subscribe(DeviceDataListener deviceDataListener) {
        Log.i(TAG, "subscribe: " + getLocalClassName());
        RxBus.getInstance().toObservable(BleData.class).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<BleData>() {
            @Override
            public void onSubscribe(Disposable d) {
                disposable = d;
            }

            @Override
            public void onNext(BleData bleData) {
                String action = bleData.getAction();
                if (action.equals(ActionData.BleData)) {
                    byte[] value = bleData.getValue();
                    ResolveUtil.postData(value, deviceDataListener);
                }else if(action.equals(ActionData.BackToHome)){
                    backToHome();
                }else if(action.equals(ActionData.StopMeasure)){
                    stopMeasure();
                }else if(action.equals(ActionData.ActionRegisterFinish)){
                   finishRegister();
                }else if(action.equals(OtaActivity.Action_Dfu_Complete)){
                    otaFinish();
                }

            }


            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void finishRegister() {
        if(this instanceof BindTipsActivity){
            //startActivity(MainActivity.class);
            finish();
        }

    }

    protected void otaFinish() {

    }

    protected void backToHome() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unSubscribe(disposable);
        KeepScreenOff();
        WebSocketUtils.getInstance().setMeasuring(false);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ActivityCompat.finishAfterTransition(this);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    protected void unSubscribe(Disposable disposable) {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
    }

    @Override
    public void DeviceAddress(String address) {

    }

    @Override
    public void DeviceVersion(String version) {

    }

    @Override
    public void DeviceTime(Calendar calendar) {

    }

    @Override
    public void DeviceUserInfo(WatchUserInfo watchUserInfo) {

    }

    @Override
    public void RealTimeData(RealTimeStepData timeStepData) {

    }

    @Override
    public void PPGMeasureData(PPGMeasureData ppgMeasureData) {

    }

    @Override
    public void EcgMeasureData(EcgMeasureResult ecgMeasureResult) {

    }

    @Override
    public void SleepData(SleepData sleepData,boolean finish) {
        if(sleepData!=null) DataDbHelper.getInstance(this).getSleepDataDao().insert(sleepData);
    }

    @Override
    public void TempData(float tempData) {

    }


    protected void KeepScreenOn() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    protected void KeepScreenOff() {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    protected void stopMeasure(){

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.i(TAG, "onKeyDown: " + keyCode);
        switch (keyCode) {
            case KeyEvent.KEYCODE_HOME:
                Log.i(TAG, "onKeyDown: home");
                if (this instanceof MainActivity) {
                    RxBus.getInstance().sendAction(ActionData.BackToHome);
                } else {
                    finish();
                }
                break;
            case KeyEvent.KEYCODE_BACK:
                Log.i(TAG, "onKeyDown: back");
                if (this instanceof MainActivity) {

                } else {
                    startActivity(MainActivity.class);
                    finish();
                }
                RxBus.getInstance().sendAction(ActionData.BackToHome);
                return true;
            case KeyEvent.KEYCODE_F2:


                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}

